/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vendingmachine;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 *
 * Nama File    : ProductSelector.Java
 * Deskripsi    : deskripsi dan implementasi kelas ProductSelector yang merupakan alat untuk memilih produk yang akan dibeli
 * Nama Pembuat : Muhammad Ghufron Mahfudhi (23512066) dan Jonathan Ery Pradana (23512038)
 * Hari/Tanggal : Senin / 26 November 2012
 */
public class ProductSelector {
    
    private boolean available;
    private int selectionId;

    public ProductSelector() throws FileExp {
        //this.available = false;
        
        
        this.selectionId = -1; //default
    }

    public boolean isAvailable(ObjectReceiver objReceiver) {
        available = objReceiver.isValid();
        return available;
    }

    public void setAvailable() {
        this.available = true;
    }
    
    public void setUnavailable(){
        this.available = false;
        this.selectionId = -1;
    }

    public int getSelectionId() {
        return selectionId;
    }

    public void setSelectionId(int selectionId) {
        this.selectionId = selectionId;
    }
    
    private boolean isSelectionValid(ProductStorage prodStorage, int id){
        if (id>=0 && id<prodStorage.getTray().size()){
            return true;
        }else{
            return false;
        }
    }
    
    public void readSelectionId(ObjectReceiver objReceiver,ProductStorage prodStorage, CoinStorage coinStorage,ProductDispenser prodDisp, ObjectDispenser objDisp){
        if(isAvailable(objReceiver)){
            if(!prodStorage.isProductsEmpty()){
                UserDisplayNotification notif = new UserDisplayNotification(UserDisplayNotification.COUNT_MONEY, "inserted", objReceiver.getStorageValue(), false);
                System.out.println("Select an available product : ");
                for(List<Product> tray: prodStorage.getTray()){
                    if(tray.size() > 0){
                        tray.get(tray.size()-1).showProduct();
                    }
                }
                System.out.print(">>");
                Scanner scan = new Scanner(System.in);
                int tempSelection = scan.nextInt() - 1;
                if(isSelectionValid(prodStorage, tempSelection)){
                    this.selectionId = tempSelection;
                    PaymentCounter payCount = new PaymentCounter(prodStorage.getPrice(selectionId),objReceiver.getStorageValue());
                    if (payCount.isSufficient()){
                        for (int i = 0; i<ObjectReceiver.getTempStorage().getNickelTray().size();i++){
                            coinStorage.getNickelTray().add(ObjectReceiver.getTempStorage().getNickelTray().remove(ObjectReceiver.getTempStorage().getNickelTray().size()-1));
                        }
                        for (int i = 0; i<ObjectReceiver.getTempStorage().getDimeTray().size();i++){
                            coinStorage.getDimeTray().add(ObjectReceiver.getTempStorage().getDimeTray().remove(ObjectReceiver.getTempStorage().getDimeTray().size()-1));
                        }
                        for (int i = 0; i<ObjectReceiver.getTempStorage().getQuarterTray().size();i++){
                            coinStorage.getQuarterTray().add(ObjectReceiver.getTempStorage().getQuarterTray().remove(ObjectReceiver.getTempStorage().getQuarterTray().size()-1));
                        }
                        objReceiver.resetStorageValue();
                        prodDisp = new ProductDispenser(prodStorage.getProduct(selectionId),prodStorage.getTray().get(selectionId));
                        if (payCount.getChange() > 0){
                            List<Coin> changes = new ArrayList<>(coinStorage.generateChangeCoin(payCount));
                            objDisp = new ObjectDispenser(changes,payCount.getChange());
                        }
                        this.setUnavailable();
                    } else {
                        notif = new UserDisplayNotification(UserDisplayNotification.UNSUFFICIENT_FUNDS);
                    }
                }else{
                    notif = new UserDisplayNotification(UserDisplayNotification.INVALID_SELECTION);
                }
            }else{
                UserDisplayNotification notif = new UserDisplayNotification(UserDisplayNotification.PRODUCT_EMPTY);
            }
        }else{
            UserDisplayNotification notif = new UserDisplayNotification(UserDisplayNotification.SELECTOR_UNAVAIL);
        }
    }
}
